package com.telit.uav.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.telit.common.entity.DataReturn;
import com.telit.common.entity.PageData;
import com.telit.common.util.JwtTokenUtils;
import com.telit.uav.entity.CommAirway;
import com.telit.uav.service.AirwayService;
import com.telit.uav.service.impl.UserInfoService;
import com.telit.upms.vo.MicroCustomRoleInfoVo;
import com.telit.upms.vo.MicroSysUserAllInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


/**
 * @Author:zhangwei
 * @Description:
 * @Time:Created on 2021/10/16 9:00
 */

@RestController
@RequestMapping("/airway")
@Api(tags = "无人机巡航-航线管理",description = "/airway")
public class AirwayController {

    @Autowired
    AirwayService airwayService;

    @Autowired
    private UserInfoService userInfoService;

    @PostMapping(value = "/add", produces = "application/json;charset=UTF-8")
    @ApiOperation("新增航线信息")
    public DataReturn add(@RequestBody CommAirway airway) {
        Long id = JwtTokenUtils.getId();
        MicroSysUserAllInfoVo userInfo = userInfoService.getUserInfo(id);
        List<MicroCustomRoleInfoVo> convertList = userInfo.getConvertList();
        airway.setCreateTime(new Date());
        airway.setUpdateTime(new Date());
        airway.setCreateUser(convertList.get(0).getRoleName() + " 【" + JwtTokenUtils.getRealName() + "】");
        airwayService.add(airway);
        return DataReturn.ok();
    }

    @PostMapping(value="/update",produces = "application/json;charset=UTF-8")
    @ApiOperation("修改航线信息")
    public DataReturn update(@RequestBody CommAirway airway){
        airway.setUpdateTime(new Date());
        airwayService.modify(airway);
        return DataReturn.ok();
    }

    @GetMapping(value="/listByPage",produces = "application/json;charset=UTF-8")
    @ApiOperation("分页条件航线信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "页码", paramType = "path", dataType = "int"),
            @ApiImplicitParam(name = "size", value = "每页条数", paramType = "path", dataType = "int")
    })
    public DataReturn listByPage(@RequestParam Integer current,
                                 @RequestParam Integer size,
                                 @ModelAttribute CommAirway airway){
        IPage<CommAirway> airwayIPage=airwayService.selectByPage(current,size,airway);
        return DataReturn.ok(new PageData<>((int)airwayIPage.getTotal(),current,size,airwayIPage.getRecords()));
    }

    @GetMapping(value="/list",produces = "application/json;charset=UTF-8")
    @ApiOperation("无分页条件查询航线信息")
    public DataReturn list(@ModelAttribute CommAirway airway){
        List<CommAirway> airwayList=airwayService.selectByNoPage(airway);
        return DataReturn.ok(airwayList);
    }

    @GetMapping(value="/queryById/{id}",produces = "application/json;charset=UTF-8")
    @ApiOperation("根据id查询航线信息")
    public DataReturn queryById(@PathVariable("id") Long id){
        return DataReturn.ok(airwayService.queryById(id));
    }

    @GetMapping("/delete/{id}")
    @ApiOperation(value = "根据ID删除航线信息")
    public DataReturn delete(@PathVariable("id") Long id){

        airwayService.deleteById(id);

        return DataReturn.ok();
    }


}
